export async function exec<T>(method: string, path: string): Promise<T> {
  const { href } = window.location
  const prefix = /^https?/.test(href) ? '' : 'https://smartx.one'
  const r = await fetch(prefix + path, { method })
  try {
      const { success, message, result } = await r.json()
      console.log(success);
      console.log(message);
      console.log(result);
    //return success ? result : Promise.reject(new Error(message))
    return success ? result : Promise.reject(null);
  } catch (e) {
    //return Promise.reject(new Error(`Error: ${r.status} ${r.statusText}`))
      return Promise.reject(null);
  }
}

export async function exec2<T>(method: string, path: string): Promise<T> {
    const { href } = window.location
    const prefix = /^https?/.test(href) ? '' : 'https://smartx.one'
    const r = await fetch(prefix + path, { method })
    try {
        return await r.json();
    } catch (e) {
        //return Promise.reject(new Error(`Error: ${r.status} ${r.statusText}`))
        return Promise.reject(null);
    }
}

export async function geturl<T>(url: string, params: any): Promise<T> {

    var str = '';
    if (typeof params === 'object' && params) {

        if (url.indexOf('?')==-1)
            str += '?';
        else
            str += '&';

        Object.keys(params).forEach(function (val) {
            str += val + '=' + encodeURIComponent(params[val]) + '&';
        })
    }
    console.log("tran==>发送交易的url是"+url + str);
    const res = await fetch(url + str, { method: 'GET' })

    try {
        console.log('geturl1');
        return await res.json();
    } catch (e) {
        return Promise.reject(null);
    }
}




